package influx;

import catchProcess.CatchProcess;
import jmx.JMX;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;


/**
 * @author Administrator
 */
public class DataInsertInflux {

    public void insertInflux() throws Exception {
        CatchProcess cp = new CatchProcess();

        //和influxdb进行连接
        InfluxdbCon influxdbcon = new InfluxdbCon();
        InfluxdbService influxSer = influxdbcon.influxCon();


        //加载hostname文件获取hostname
        String hostname;
        try (BufferedReader br = new BufferedReader(new FileReader("/etc/hostname"))) {
            hostname = br.readLine();
        }

        //各种有索引的属性
        Map<String, String> tags = new HashMap<String, String>();
        tags.put("hostname", hostname);
        //通过jmx获取指标信息
        Map<String, Object> fields = JMX.getMetrics("/cassandra-agent/conf/cassandraMbean.xml");
        //向数据库中插入进程状态
        fields.put("cassandra", cp.catchCassandraPro());
        //向cassandra表中插入数据
        influxSer.insert("cassandra", tags, fields);
        System.out.println("insert successful!");
    }
}
